package ext;

import image.ImageGroup;
import image.ps.PixelShader;
import math.vec2i;
import math.vec3;

public class PSMerge extends PixelShader
{
    
    public static final int sMERGE_NONE = -1;
    
    public static final int sMERGE_ADD = 0;
    
    public static final int sMERGE_SUB = 1;
    
    public static final int sMERGE_MUL = 2;
    
    public static final int sMERGE_DIV = 3;
    
    public static final int sMERGE_AVG = 4;
    
    
    public int mMergeOp;
    
    public int mTargetI3_A;
    
    public int mTargetI3_B;
    
    public int mTargetI3_OUT;
    
    
    public PSMerge()
    {
        super();
        this.mMergeOp = -1;
        this.mTargetI3_A = -1;
        this.mTargetI3_B = -1;
        this.mTargetI3_OUT = -1;
    }
    
    
    public PixelShader copy()
    {
        PSMerge copy = new PSMerge();
        copy.mMergeOp = this.mMergeOp;
        copy.mTargetI3_A = this.mTargetI3_A;
        copy.mTargetI3_B = this.mTargetI3_B;
        copy.mTargetI3_OUT = this.mTargetI3_OUT;
        return copy;
    }
    
    
    public void shade(
            ImageGroup aTarget, 
            vec2i aImagePixel, 
            vec2i aImageSize)
    {
        vec3 datumA = aTarget.image3(this.mTargetI3_A).read(aImagePixel);
        vec3 datumB = aTarget.image3(this.mTargetI3_B).read(aImagePixel);
        
        vec3 datumWrite;
        if (this.mMergeOp == PSMerge.sMERGE_ADD)
        {
            datumWrite = vec3.add(datumA, datumB);
        }
        else if (this.mMergeOp == PSMerge.sMERGE_SUB)
        {
            datumWrite = vec3.sub(datumA, datumB);
        }
        else if (this.mMergeOp == PSMerge.sMERGE_MUL)
        {
            datumWrite = vec3.mul(datumA, datumB);
        }
        else if (this.mMergeOp == PSMerge.sMERGE_DIV)
        {
            datumWrite = vec3.div(datumA, datumB);
        }
        else if (this.mMergeOp == PSMerge.sMERGE_AVG)
        {
            datumWrite = vec3.mul(0.5, vec3.add(datumA, datumB));
        }
        else // this.mMergeOp == PSMerge.sMERGE_NONE
        {
            datumWrite = datumA;
        }
        
        aTarget.image3(this.mTargetI3_OUT).write(aImagePixel, datumWrite);
    }
    
}
